/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vng.survey.controller;


import com.vng.zing.survey.thrift.client.SurveyClient;
import com.vng.zing.survey.thrift.model.ShortInfoSurveyItem;
import com.vng.zing.survey.thrift.model.UserType;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vng.survey.model.CommonModel;
import vng.survey.model.RenderModel;


/**
 *
 * @author namhh
 */
public class HomeController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(HomeController.class);

    @Override
    public String render(HttpServletRequest req, HttpServletResponse resp) throws IOException, Exception {

        String content = "";
        int idUser = 1;

//        String cached = (String) getCache(new String[]{String.format("%s", idUser)});
//        if (cached != null && "".equals(cached)) {
//            return cached;
//        }
        TemplateDataDictionary dic = TemplateDictionary.create();

        dic.setVariable("HEADER", CommonModel.getHeader(TemplateDictionary.create(), "Trang khảo sát"));
        dic.setVariable("FOOTER", CommonModel.getFooter(TemplateDictionary.create()));



        SurveyClient surveyClient = SurveyClient.getInstance();
         List<ShortInfoSurveyItem> newestSurvey;
         int count=0;
        if(1 == 1){
             newestSurvey  = surveyClient.getListShortInfoSurveyItem(1, 0, CommonModel.OFFSET,UserType.ADMIN);
             count=  surveyClient.getCountSurvery(1);
        }
        else{
             newestSurvey  = surveyClient.getListShortInfoSurveyItem(1, 0, CommonModel.OFFSET,UserType.USER);
               count=  surveyClient.getCountSurveyNew(1);
        }
        


        TemplateDataDictionary dicNewTable = TemplateDictionary.create();
        if (newestSurvey.isEmpty()) {
            dicNewTable.setVariable("CONTENT", "Chưa có khảo sát nào!");
            content = CommonModel.getEmpty(dicNewTable);
        } else {
            for (ShortInfoSurveyItem t : newestSurvey) {
                TemplateDataDictionary slideItems = dicNewTable.addSection("SLICE_SHOW_ITEMS");
                slideItems.setVariable("RESPONSE_LINK", "response?id=" + t.getIdSurvey());
                slideItems.setVariable("TITLE", t.getTitle());
                slideItems.setVariable("VIEW", String.format("%s", t.getView()));
                slideItems.setVariable("RESPONSE", String.format("%s", t.getResponse()));
                slideItems.setVariable("LINK", "stat?id=" + t.getIdSurvey());
            }

            content = CommonModel.getNewTable(dicNewTable);
            int page = (int) Math.floor( count/ CommonModel.OFFSET);
            System.out.println("count"+ page);

            if (page > 1) {
                TemplateDataDictionary dicPage = TemplateDictionary.create();
                dicPage.setVariable("COUNT", String.format("%s", page));
                dicPage.setVariable("OFFSET", String.format("%s", CommonModel.OFFSET));
                dicPage.setVariable("NAME", "paging_new");
                String paging = CommonModel.getPaging(dicPage);
                dic.setVariable("PAGE", paging);
            }
        }

        dic.setVariable("NEW_TABLE", content);
        // host
        TemplateDataDictionary dicHostTable = TemplateDictionary.create();
        newestSurvey = surveyClient.getListShortInfoSurveyItemHost(1, 0, CommonModel.OFFSET);
        if (newestSurvey.isEmpty()) {
            dicHostTable.setVariable("CONTENT", "Chưa có khảo sát nào!");
            content = CommonModel.getEmpty(dicHostTable);
        } else {
            for (ShortInfoSurveyItem t : newestSurvey) {
                TemplateDataDictionary slideItems = dicHostTable.addSection("SLICE_SHOW_ITEMS");
                slideItems.setVariable("RESPONSE_LINK", "response?id=" + t.getIdSurvey());
                slideItems.setVariable("TITLE", t.getTitle());
                slideItems.setVariable("VIEW", String.format("%s", t.getView()));
                slideItems.setVariable("RESPONSE", String.format("%s", t.getResponse()));
                slideItems.setVariable("LINK", "stat?id=" + t.getIdSurvey());
            }

            content = CommonModel.getHotTable(dicHostTable);
            int page = (int) Math.floor(surveyClient.getCountSurveyHost(1) / CommonModel.OFFSET);
            if (page > 1) {
                TemplateDataDictionary dicPage = TemplateDictionary.create();
                dicPage.setVariable("COUNT", String.format("%s", page+1));
                dicPage.setVariable("OFFSET", String.format("%s", CommonModel.OFFSET));
                dicPage.setVariable("NAME", "paging_hot");
                String paging = CommonModel.getPaging(dicPage);
                dic.setVariable("PAGEHOST", paging);
            }
        }
        dic.setVariable("HOT_TABLE", content);

        dic.setVariable("LINKNEW", "new");
        Template template = RenderModel.getCTemplate("home");
        content = template.renderToString(dic);
        setCache(new String[]{String.format("%s", idUser)}, 10, content);
        return content;
    }

}
